#!/usr/bin/env bash
set -e

# modified from https://github.com/foundry-rs/foundry/blob/master/foundryup/foundryup

REPO="a16z/helios"
NAME=helios

DIR=$HOME/.$NAME
BIN_DIR=$DIR/bin

# delete existing binaries
rm -f $BIN_DIR/$NAME

TAG=$(curl https://api.github.com/repos/$REPO/releases/latest | grep -o '"tag_name": "[^"]*' | grep -o '[^"]*$')

PLATFORM="$(uname -s)"
case $PLATFORM in
  Linux)
    PLATFORM="linux"
    ;;
  Darwin)
    PLATFORM="darwin"
    ;;
  *)
    err "unsupported platform: $PLATFORM"
    ;;
esac

ARCHITECTURE="$(uname -m)"
if [ "${ARCHITECTURE}" = "x86_64" ]; then
  # Redirect stderr to /dev/null to avoid printing errors if non Rosetta.
  if [ "$(sysctl -n sysctl.proc_translated 2>/dev/null)" = "1" ]; then
    ARCHITECTURE="arm64" # Rosetta.
  else
    ARCHITECTURE="amd64" # Intel.
  fi
elif [ "${ARCHITECTURE}" = "arm64" ] ||[ "${ARCHITECTURE}" = "aarch64" ] ; then
  ARCHITECTURE="arm64" # Arm.
else
  ARCHITECTURE="amd64" # Amd.
fi

TARBALL_URL="https://github.com/$REPO/releases/download/${TAG}/${NAME}_${PLATFORM}_${ARCHITECTURE}.tar.gz"
curl -L $TARBALL_URL | tar -xzC $BIN_DIR

echo "Installed $NAME" 
